package com.yingxin.yxerp.module.inform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yingxin.yx.framework.commons.dto.PageOut;
import com.yingxin.yx.framework.commons.enums.BusinessType;
import com.yingxin.yx.framework.mybatisplus.PageHelper;
import com.yingxin.yxerp.module.attachment.service.IAttachmentService;
import com.yingxin.yxerp.module.inform.entity.Notice;
import com.yingxin.yxerp.module.inform.entity.builder.NoticeBuilder;
import com.yingxin.yxerp.module.inform.service.INoticeService;
import com.yingxin.yxerp.module.inform.vo.req.NoticeCreateVo;
import com.yingxin.yxerp.module.inform.vo.req.NoticeQueryVo;
import com.yingxin.yxerp.module.inform.vo.req.NoticeUpdateVo;
import com.yingxin.yxerp.module.inform.vo.resp.NoticeRespVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 通知公告-公告 前端控制器
 * @author zhuchangjie
 * @since 2021-12-21
 */

@RestController
@RequestMapping("/inform/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;

    @Autowired
    private IAttachmentService attachmentService;

    @PreAuthorize("hasAuthority('notice:view')")
    @ApiOperation(value = "查询列表（分页）")
    @PostMapping("/page")
    public PageOut<NoticeRespVo> page(@RequestBody NoticeQueryVo queryVo) {
        IPage<NoticeRespVo> page = noticeService.pageVo(PageHelper.in(queryVo), queryVo);
        return PageHelper.out(page);
    }

    @PreAuthorize("hasAuthority('notice:view')")
    @ApiOperation(value = "查询列表（全部）")
    @PostMapping("/list")
    public List<NoticeRespVo> list(@RequestBody NoticeQueryVo queryVo) {
        return noticeService.listVo(queryVo);
    }

    @ApiOperation(value = "查询列表（分页） - app")
    @PostMapping("/pageApp")
    public PageOut<NoticeRespVo> pageApp(@RequestBody NoticeQueryVo queryVo) {
        IPage<NoticeRespVo> page = noticeService.pageAppVo( PageHelper.in(queryVo), queryVo );
        return PageHelper.out(page);
    }

    @ApiOperation(value = "查询列表（全部） - app")
    @PostMapping("/listApp")
    public List<NoticeRespVo> listApp(@RequestBody NoticeQueryVo queryVo) {
        return noticeService.listAppVo( queryVo );
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public NoticeRespVo get(@PathVariable("id") String id) {
        NoticeRespVo respVo = noticeService.getVo(id);
        respVo.setAttachmentList(attachmentService.listByBiz(id, BusinessType.TZGG));
        return respVo;
    }

    @PreAuthorize("hasAuthority('notice:create')")
    @ApiOperation(value = "新增")
    @PostMapping
    public String create(@RequestBody @Valid NoticeCreateVo createVo) {
        Notice notice = NoticeBuilder.build(createVo);
        noticeService.create(notice);
        return notice.getId();
    }

    @PreAuthorize("hasAuthority('notice:update')")
    @ApiOperation(value = "修改")
    @PutMapping
    public String update(@RequestBody @Valid NoticeUpdateVo updateVo) {
        Notice notice = NoticeBuilder.build(updateVo);
        noticeService.update(notice);
        return notice.getId();
    }

    @PreAuthorize("hasAuthority('notice:delete')")
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        Notice notice = noticeService.getById(id);
        noticeService.delete(notice);
    }

}
